package ar.edu.ufasta.presupuestos.logica.aplicativo;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class ModeloTablaGenerica extends AbstractTableModel {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused")
	private String[] columnMapList; 
	@SuppressWarnings("unused")
	private Vector<Object[]> recordList;
	//private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.getDefault());
	private boolean isEditable=false; 
	
	/**
	 * @return the recordList
	 */
	public Vector<Object[]> getRecordList() {
		return recordList;
	}

	/**
	 * Controller is responsible to set data
	 * Vista is responsible to set column map before rendering JTable
	 */
	public ModeloTablaGenerica(String[] columns){
		columnMapList = columns;
		recordList = new Vector<Object[]>();
	}
	
	public void updateOrigen(Vector<Object[]> records){
		recordList = records;
		fireTableDataChanged();
	}

	public void resetOrigen(){
		recordList = new Vector<Object[]>();
	}
	
	/**
	 * Devuelve las columnas de la tabla
	 * @return Cantidad de columnas de la tabla
	 */

	public int getColumnCount() {
		return this.columnMapList.length;
	}

	/**
	 * Devuelve los nombre de cada columna de la tabla
	 * @param column
	 * @return Nombre de la columna
	 */
	@Override
	public String getColumnName(int column){
		return (String) columnMapList[column];
	}
	
	/**
	 * Devuelve la cantidad de registros de la tabla
	 * @return Cantidad de filas
	 */

	public int getRowCount() {
		return this.recordList.size();
	}

	/**
	 * Obtiene un objeto de la tabla
	 * @param arg0
	 * @param arg1
	 * @return Objeto encontrado en Fila/Columna
	 */

	public Object getValueAt(int arg0, int arg1) {
		Object ret = null;
		try {
			ret = this.recordList.elementAt(arg0)[arg1];
		} catch (Exception e) {

		}
		return ret;
	}

	public void setRecordList(Vector<Object[]> recordList) {
		this.recordList = recordList;
	}

	public void setColumnMapList(String[] columnMapList) {
		this.columnMapList = columnMapList;
	}

	/*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
    	Class ca = getValueAt(0, c)!=null? getValueAt(0, c).getClass():null; 
        return ca;
    }

	
	/**
	 * @return the isEditable
	 */
	public boolean isEditable() {
		return isEditable;
	}

	/**
	 * @param isEditable the isEditable to set
	 */
	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
	}

	public boolean isCellEditable(int row,int col){
		 //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if ((isEditable)&&(col==4)){
        	return true;
        }else{
        	return false;
        }        
	}
	
	/*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");


            Object[] rec = recordList.get(row);
            rec[col] = value;
            recordList.remove(row);
            recordList.add(row, rec);
            //recordList[row][col] = value;
        fireTableCellUpdated(row, col);


         //   System.out.println("New value of data:");
         //   printDebugData();

    }

}
